package xcmg.device.service.delivery;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xcmg.device.dao.entity.boxup.DeliveryReceiptDetailDO;
import xcmg.device.dao.entity.delivery.DeliveryReceiptDO;
import xcmg.device.dao.entity.delivery.DeliveryReceiptLinkDO;
import xcmg.device.dao.entity.delivery.DeliveryReceiptLogisticsDO;
import xcmg.device.dao.entity.logistics.SendBillDO;
import xcmg.device.dao.entity.purchase.PurchaseRequestDO;
import xcmg.device.dao.mapper.DeliveryReceiptDetailMapper;
import xcmg.device.dao.mapper.PurchaseRequestMapper;
import xcmg.device.dao.mapper.delivery.DeliveryReceiptLinkMapper;
import xcmg.device.dao.mapper.delivery.DeliveryReceiptLogisticsMapper;
import xcmg.device.dao.mapper.delivery.DeliveryReceiptMapper;
import xcmg.device.dao.mapper.logistics.SendBillMapper;
import xcmg.device.infra.BeanUtil;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.vo.delivery.*;
import xcmg.device.service.vo.logistics.SendBillAddVO;
import xcmg.device.service.vo.logistics.SendBillVO;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.infra.ActionResult;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class DeliveryReceiptLogisticsService {

    @Autowired
    private DeliveryReceiptLogisticsMapper deliveryReceiptLogisticsMapper;

    @Autowired
    private DeliveryReceiptMapper deliveryReceiptMapper;

    @Autowired
    private DeliveryReceiptDetailMapper deliveryReceiptDetailMapper;

    @Autowired
    private FastGenClient fastGenClient;

    @Autowired
    private PurchaseRequestMapper purchaseRequestMapper;

    @Autowired
    private DeliveryReceiptLinkMapper deliveryReceiptLinkMapper;

    @Autowired
    private SendBillMapper sendBillMapper;

    public ErrorCode add(DeliveryReceiptLogisticsVO deliveryReceiptLogisticsVO){
         ActionResult<String> textResult=fastGenClient.textGuid();
        if(textResult.getCode() != ErrorCode.Success.getCode()){
            return ErrorCode.IllegalArument;
        }
        DeliveryReceiptLogisticsDO logisticsDO = new DeliveryReceiptLogisticsDO();
        logisticsDO.setId(textResult.getValue());
        logisticsDO.setBillId(deliveryReceiptLogisticsVO.getBillId());
        logisticsDO.setBillNo(deliveryReceiptLogisticsVO.getBillNo());
        logisticsDO.setCreateId(deliveryReceiptLogisticsVO.getCreateId());
        logisticsDO.setCreateDate(new Date());
        logisticsDO.setAddress(deliveryReceiptLogisticsVO.getAddress());
        deliveryReceiptLogisticsMapper.add(logisticsDO);
        return ErrorCode.Success;
    }

    public ErrorCode remove(List<String> ids){
        for(String id:ids){
            deliveryReceiptLogisticsMapper.delete(id);
        }
        return ErrorCode.Success;
    }

    /**
     * 判断是否紧急：对应的采购订单，如果其中有一个是紧急，则打印的发运交接单显示紧急
     *
     * @param deliveryReceiptId
     * @return
     */
    private boolean isUrgent(String deliveryReceiptId) {
        List<DeliveryReceiptLinkDO> linkDOS = deliveryReceiptLinkMapper.list(deliveryReceiptId);
        if (CollectionUtils.isNotEmpty(linkDOS)) {
            for (DeliveryReceiptLinkDO linkDO : linkDOS) {
                PurchaseRequestDO requestDO = purchaseRequestMapper.item(linkDO.getPurchaseBillId());
                if (null != requestDO && requestDO.isUrgent()) {
                    return true;
                }
            }
        }
        return false;
    }

    public DeliveryReceiptAddVO item(String billNo){
        DeliveryReceiptAddVO result = new DeliveryReceiptAddVO();
        DeliveryReceiptDO deliveryReceiptDO = deliveryReceiptMapper.itemByNo(billNo);
        if(null == deliveryReceiptDO){
            return result;
        }

        DeliveryReceiptVO deliveryReceiptVO = new DeliveryReceiptVO();
        int recordCount = deliveryReceiptLogisticsMapper.countByDate(billNo, DateFormatUtils.format(new Date(), "yyyyMMdd"));
        deliveryReceiptVO.setRecordFlag(recordCount > 0 ? DeliveryReceiptVO.RECORD_FLAG_YES : DeliveryReceiptVO.RECORD_FLAG_NO);
        BeanUtils.copyProperties(deliveryReceiptDO,deliveryReceiptVO);
        List<DeliveryReceiptDetailDO> detailDOList=deliveryReceiptDetailMapper.detalList(deliveryReceiptDO.getId());
        List<DeliveryReceiptDetailVO> detailVOList=new ArrayList<>();
        for (DeliveryReceiptDetailDO detailDO:detailDOList
             ) {
            DeliveryReceiptDetailVO detailVO=new DeliveryReceiptDetailVO();
            BeanUtils.copyProperties(detailDO,detailVO);
            detailVOList.add(detailVO);
        }
        result.setDetailVOList(detailVOList);
        result.setDeliveryReceiptVO(deliveryReceiptVO);
        List<DeliveryReceiptLogisticsVO> listVO = new ArrayList<>();
        List<DeliveryReceiptLogisticsDO> listDO = deliveryReceiptLogisticsMapper.list(billNo);
        for(DeliveryReceiptLogisticsDO entity:listDO) {
            DeliveryReceiptLogisticsVO model = new DeliveryReceiptLogisticsVO();
            BeanUtils.copyProperties(entity, model);
            listVO.add(model);
        }

        result.setDeliveryList(listVO);
        return result;
    }

    public SendBillAddVO item4Logistics(String sendOrder){
        SendBillAddVO result = new SendBillAddVO();
        SendBillDO sendBillDO = sendBillMapper.itemByBillNo(sendOrder);
        if(null == sendBillDO){
            return result;
        }

        SendBillVO sendBillVO = BeanUtil.do2bo(sendBillDO, SendBillVO.class);
        int recordCount = deliveryReceiptLogisticsMapper.countByDate(sendOrder, DateFormatUtils.format(new Date(), "yyyyMMdd"));
        sendBillVO.setRecordFlag(recordCount > 0 ? 1 : 0);
        result.setHeaderInfo(sendBillVO);
        //物流列表
        List<DeliveryReceiptLogisticsDO> listDO = deliveryReceiptLogisticsMapper.list(sendOrder);
        List<DeliveryReceiptLogisticsVO> logisticsVOList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(listDO)){
            logisticsVOList = BeanUtil.do2bo4List(listDO, DeliveryReceiptLogisticsVO.class);
        }
        result.setLogisticsVOList(logisticsVOList);
        return result;
    }


}
